//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.ky;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.CardTypeCodeDao;
import com.aidong.dao.wills.DicShopAreaCodeDao;
import com.aidong.dao.wills.KyBespeakDao;
import com.aidong.dao.wills.KyContractDao;
import com.aidong.dao.wills.KyTimeTableDao;
import com.aidong.dao.wills.MemberContractDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.dao.wills.PtItemCodeDao;
import com.aidong.dao.wills.PtPriceDao;
import com.aidong.model.wills.CardTypeCode;
import com.aidong.model.wills.KyBespeak;
import com.aidong.model.wills.KyContract;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PtPrice;
import com.aidong.web.controller.member.PayController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"kyBespeak"})
public class KyBespeakController extends PayController {
    @Resource
    KyTimeTableDao kyTimeTableDao;
    @Resource
    KyBespeakDao kyBespeakDao;
    @Resource
    KyContractDao kyContractDao;
    @Resource
    PtItemCodeDao ptItemCodeDao;
    @Resource
    PtCoachCodeDao ptCoachCodeDao;
    @Resource
    PtPriceDao ptPriceDao;
    @Resource
    protected MemberInfoDao memberInfoDao;
    @Resource
    MemberContractDao memberContractDao;
    @Resource
    CardTypeCodeDao cardTypeCodeDao;
    @Resource
    DicShopAreaCodeDao dicShopAreaCodeDao;

    public KyBespeakController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return (new ModelAndView("ky/bespeak/list")).addObject("dt", sdf.format(new Date()));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "cardCode",required = false) String cardCode, @RequestParam(value = "mobile",required = false) String mobile, @RequestParam(value = "classType",required = false) String classType, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.eq("shopCode", this.getLoginShopCode());
        if(!this.chkNull(status)) {
            cd.eq("status", status);
        } else {
            cd.gt("status", Integer.valueOf(0));
        }

        cd.eq("classType", classType);
        cd.eq("cardCode", cardCode);
        cd.eq("mobile", mobile);
        cd.gt("classTime", this.getTrimDate(new Date()));
        this.kyBespeakDao.queryPageList(pg, cd, (new OrderSet()).asc("classTime"));
        ModelAndView mv = new ModelAndView("ky/bespeak/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        mv.addObject("nowDate", new Date());
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        List itemList = this.ptItemCodeDao.getShopItemList(this.getLoginShopCode(), "06");
        List coachList = this.ptCoachCodeDao.getCoachListByType("04");
        ModelAndView mv = new ModelAndView("ky/bespeak/add");
        mv.addObject("itemList", itemList);
        mv.addObject("coachList", coachList);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("table") KyBespeak bespeak, @RequestParam("date") String date, @RequestParam("hh") String hh, @RequestParam("mm") String mm) {
        if(this.chkNull(bespeak.getPtCode())) {
            return this.getResult(false, "请选择课程");
        } else if(this.chkNull(bespeak.getCoachCode())) {
            return this.getResult(false, "请选择教练");
        } else if(!this.chkNull(date) && !this.chkNull(hh) && !this.chkNull(mm)) {
            if(hh.length() == 1) {
                hh = "0" + hh;
            }

            if(mm.length() == 1) {
                mm = "0" + mm;
            }

            bespeak.setClassTime(this.parseDate(date + " " + hh + ":" + mm + ":00", "yyyy-MM-dd HH:mm:ss"));
            if(bespeak.getClassTime() != null && !bespeak.getClassTime().before(this.getTrimDate(new Date()))) {
                bespeak.setShopCode(this.getLoginShopCode());
                bespeak.setBespeakDate(new Date());
                bespeak.setClassType("1");
                PtPrice price = this.ptPriceDao.getByCoachLevel(bespeak.getPtCode(), "01", 1);
                if(price == null) {
                    return this.getResult(false, "不能购买");
                } else if(this.chkNull(bespeak.getCardCode())) {
                    return this.getResult(false, "请输入K-Yoga卡号");
                } else {
                    MemberInfo member = this.memberInfoDao.getByKyogaCard(bespeak.getCardCode());
                    if(member == null) {
                        return this.getResult(false, "K-Yoga卡号错误");
                    } else {
                        KyContract contract = this.kyContractDao.getValidAmountByMemberCode(member.getMemberCode(), bespeak.getShopCode());
                        if(contract != null && contract.getEndTime() != null) {
                            if(contract.getEndTime().before(this.getTrimDate(new Date()))) {
                                return this.getResult(false, "K-Yoga合同已过有效期");
                            } else if(contract.getFactAmount().doubleValue() + contract.getPresentAmount().doubleValue() - price.getPriceMin().doubleValue() < 0.0D) {
                                return this.getResult(false, "会员剩余金额不足");
                            } else {
                                bespeak.setCardCode(member.getKyogaCard());
                                bespeak.setMemberCode(member.getMemberCode());
                                bespeak.setCustName(member.getChName());
                                bespeak.setMobile(member.getMobile());
                                bespeak.setFactPrice(price.getPriceMin());
                                bespeak.setStatus(Integer.valueOf(2));
                                bespeak.setSalesCode(bespeak.getCoachCode());
                                bespeak.setCreateUser(this.getLoginUserCode());
                                bespeak.setCreateTime(new Date());
                                bespeak.setUpdateTime(new Date());

                                try {
                                    this.kyBespeakDao.addBespeak(bespeak);
                                    this.addUserLog("预约K_Yoga团操", "编号:" + bespeak.getClassId() + ",课程:" + this.cache.getValue("ptItemName", bespeak.getPtCode()) + ",姓名:" + bespeak.getCustName());
                                } catch (Exception var9) {
                                    this.log.error("add kybespeak", var9);
                                    return this.getResult(false, "保存发生错误");
                                }

                                return this.getResult(true, "预约成功");
                            }
                        } else {
                            return this.getResult(false, "会员没有有效的K-Yoga销售合同");
                        }
                    }
                }
            } else {
                return this.getResult(false, "不能预约已过的上课时间");
            }
        } else {
            return this.getResult(false, "请输入上课时间");
        }
    }

    @RequestMapping({"_clear"})
    @ResponseBody
    public String _clear(@RequestParam("id") int id) {
        KyBespeak bespeak = (KyBespeak)this.kyBespeakDao.getById(Integer.valueOf(id));
        if(bespeak != null && bespeak.getStatus().intValue() != 0) {
            if(bespeak.getStatus().intValue() == 1) {
                return this.getResult(false, "已经核销");
            } else if(bespeak.getStatus().intValue() != 2) {
                return this.getResult(false, "不能核销");
            } else {
                bespeak.setStatus(Integer.valueOf(1));
                bespeak.setClearUser(this.getLoginUserCode());
                bespeak.setClearTime(new Date());
                List list = this.kyContractDao.getValidListByMemberCode(bespeak.getMemberCode(), this.getLoginShopCode());
                double money = bespeak.getFactPrice().doubleValue();
                ArrayList list1 = new ArrayList();

                KyContract contract;
                for(Iterator e = list.iterator(); e.hasNext(); list1.add(contract)) {
                    contract = (KyContract)e.next();
                    if(money <= 0.0D) {
                        break;
                    }

                    if(contract.getFactAmount().doubleValue() + contract.getPresentAmount().doubleValue() - contract.getUseAmount().doubleValue() >= money) {
                        contract.setUseAmount(Double.valueOf(contract.getUseAmount().doubleValue() + money));
                        money = 0.0D;
                    } else {
                        money -= contract.getFactAmount().doubleValue() + contract.getPresentAmount().doubleValue() - contract.getUseAmount().doubleValue();
                        contract.setUseAmount(Double.valueOf(contract.getFactAmount().doubleValue() + contract.getPresentAmount().doubleValue()));
                    }
                }

                if(list1.size() > 0) {
                    bespeak.setContractCode(((KyContract)list1.get(0)).getContractCode());
                }

                try {
                    this.kyBespeakDao.updateClear(bespeak, list1);
                    this.addUserLog("K_Yoga团操核销", "编号:" + bespeak.getAutoId() + ",课程:" + this.cache.getValue("ptItemName", bespeak.getPtCode()) + ",姓名:" + bespeak.getCustName());
                } catch (Exception var9) {
                    this.log.error("kybespeak clear", var9);
                    return this.getResult(false, "核销发生错误");
                }

                return this.getResult(true, "核销成功");
            }
        } else {
            return this.getResult(false, "没有这个预约");
        }
    }

    @RequestMapping({"_cancel"})
    @ResponseBody
    public String _cancel(@RequestParam("id") int id) {
        KyBespeak bespeak = (KyBespeak)this.kyBespeakDao.getById(Integer.valueOf(id));
        if(bespeak != null && bespeak.getStatus().intValue() != 0) {
            if(bespeak.getStatus().intValue() == 1) {
                return this.getResult(false, "已经核销,不能取消");
            } else if(bespeak.getStatus().intValue() != 2) {
                return this.getResult(false, "不能取消");
            } else {
                bespeak.setStatus(Integer.valueOf(0));
                bespeak.setCanceType("1");
                bespeak.setCancelUser(this.getLoginUserCode());
                bespeak.setCreateTime(new Date());

                try {
                    this.kyBespeakDao.updateCancel(bespeak);
                    this.addUserLog("K_Yoga团操取消", "编号:" + bespeak.getAutoId() + ",课程:" + this.cache.getValue("ptItemName", bespeak.getPtCode()) + ",姓名:" + bespeak.getCustName());
                } catch (Exception var4) {
                    this.log.error("kybespeak cancel", var4);
                    return this.getResult(false, "取消发生错误");
                }

                return this.getResult(true, "取消成功");
            }
        } else {
            return this.getResult(false, "没有这个预约");
        }
    }

    @RequestMapping({"print"})
    public ModelAndView print(@RequestParam("id") int id) {
        KyBespeak bespeak = (KyBespeak)this.kyBespeakDao.getById(Integer.valueOf(id));
        if(bespeak == null) {
            return this.showAlert("没有这个预约");
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            StringBuilder sb = new StringBuilder();
            sb.append("{saleId:").append(bespeak.getAutoId()).append("").append(",shopCode:\"").append(bespeak.getShopCode()).append("\"").append(",shopName:\"").append(this.cache.getValue("shopName", bespeak.getShopCode())).append("\"").append(",bespeakTime:\"").append(sdf.format(bespeak.getBespeakDate())).append("\"").append(",classTime:\"").append(sdf.format(bespeak.getClassTime())).append("\"").append(",ptCode:\"").append(bespeak.getPtCode()).append("\"").append(",ptCodeName:\"").append(this.cache.getValue("ptItemName", bespeak.getPtCode())).append("\"").append(",coachCode:\"").append(bespeak.getCoachCode()).append("\"").append(",coachName:\"").append(this.cache.getValue("ptCoachName", bespeak.getCoachCode())).append("\"").append(",cardCode:\"").append(bespeak.getCardCode()).append("\"").append(",memberCode:\"").append(bespeak.getMemberCode()).append("\"").append(",memberName:\"").append(bespeak.getCustName()).append("\"").append(",mobile:\"").append(bespeak.getMobile()).append("\"").append(",printDate:\"").append(sdf.format(new Date())).append("\"").append("}");
            ModelAndView mv = new ModelAndView("print/print");
            mv.addObject("data", sb.toString());
            mv.addObject("title", "团操核销");
            mv.addObject("ptjs", "gcbespeak");
            mv.addObject("_picPath", this.getSetting("pic.path"));
            return mv;
        }
    }

    @RequestMapping({"getMember2"})
    @ResponseBody
    public String getMember2(@RequestParam("cardCode") String cardCode) {
        MemberInfo member = this.memberInfoDao.getByKyogaCard(cardCode);
        if(member == null) {
            return this.getResult(false, "K-Yoga卡号错误");
        } else {
            KyContract contract = this.kyContractDao.getValidAmountByMemberCode(member.getMemberCode(), this.getLoginShopCode());
            if(contract != null && contract.getEndTime() != null) {
                if(contract.getEndTime().before(this.getTrimDate(new Date()))) {
                    return this.getResult(false, "K-Yoga合同已过有效期");
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    StringBuilder sb = new StringBuilder();
                    sb.append("{code:\"").append(member.getMemberCode()).append("\"").append(",card:\"").append(member.getKyogaCard()).append("\"").append(",shop:\"").append(member.getShopCode()).append("\"").append(",name:\"").append(member.getChName()).append("\"").append(",gender:\"").append(member.getGender()).append("\"").append(",genderName:\"").append(this.cache.getValue("gender", member.getGender())).append("\"").append(",idType:\"").append(member.getIdType()).append("\"").append(",idTypeName:\"").append(this.cache.getValue("idType", member.getIdType())).append("\"").append(",idNumber:\"").append(member.getIdNumber()).append("\"").append(",mobile:\"").append(member.getMobile()).append("\"").append(",memberPhoto:\"").append(this.nvl(member.getMemberPhoto())).append("\"").append(",idCardPhoto:\"").append(this.nvl(member.getIdCardPhoto())).append("\"").append(",memberCard:\"").append(this.nvl(member.getCardCode())).append("\"").append(",factAmount:\"").append(this.nvl(contract.getFactAmount())).append("\"").append(",presentAmount:\"").append(this.nvl(contract.getPresentAmount())).append("\"").append(",contractEnd:\"").append(contract.getEndTime() == null?"":sdf.format(contract.getEndTime())).append("\"").append("}");
                    return this.getSuccess(sb.toString(), "");
                }
            } else {
                return this.getResult(false, "会员没有有效的K-Yoga销售合同");
            }
        }
    }

    private boolean chkc(String memberCode) {
        List list = this.memberContractDao.getCurrentListByMemberCode(memberCode);
        if(list != null && list.size() != 0) {
            String shopCode = this.getLoginShopCode();
            Iterator i$ = list.iterator();

            while(i$.hasNext()) {
                MemberContract contract = (MemberContract)i$.next();
                if(shopCode.equalsIgnoreCase(contract.getShopCode())) {
                    return true;
                }

                if(this.memberContractDao.chkContractShop(contract.getContractCode(), shopCode)) {
                    return true;
                }

                if(!this.chkNull(contract.getManyShop())) {
                    CardTypeCode cardType = (CardTypeCode)this.cardTypeCodeDao.getById(contract.getCardType());
                    if(cardType != null && cardType.getManyShopQuantity().intValue() == 0 && this.dicShopAreaCodeDao.chkAreaShop(contract.getManyShop(), shopCode)) {
                        return true;
                    }
                }
            }

            return false;
        } else {
            return false;
        }
    }
}